package com.woniuxy.microcouponserver.couponInfomag.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microcommonserver.bean.coupon_pzc.CouponInfo;
import com.woniuxy.microcommonserver.pageHelper.PageRequest;
import com.woniuxy.microcommonserver.res.ResultMsg;
import com.woniuxy.microcouponserver.couponInfomag.service.ICouponInfoService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/couponinfo")
public class CouponInfoController {

    @Resource
    private ICouponInfoService couponInfoServiceImpl;

    @PostMapping(value = "/more",produces = MediaType.APPLICATION_JSON_VALUE)
    public String addCouponInfo(@RequestBody CouponInfo couponInfo){
        System.out.println(couponInfo);
        couponInfoServiceImpl.saveCouponInfo(couponInfo);
        return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("success").build());
    }


    @DeleteMapping(value = "/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg deleteCouponInfo(@PathVariable("id") Integer id){
        couponInfoServiceImpl.deletedCouponInfo(id);
        return ResultMsg.builder().code(200).msg("success").build();
    }

    @PutMapping(value = "/coupon",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg updateCouponInfo(@RequestBody CouponInfo couponInfo){
        System.out.println(couponInfo);
        couponInfoServiceImpl.updateCouponInfo(couponInfo);
        return ResultMsg.builder().code(200).msg("success").build();
    }

    @GetMapping(value = "/coupons",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg findAll(PageRequest pageRequest,CouponInfo couponInfo){
        PageInfo<CouponInfo> pageInfo = couponInfoServiceImpl.findAllByParams(couponInfo, pageRequest);
        System.out.println("=======controller:"+pageInfo);
        return ResultMsg.builder().code(200).msg("success").data(pageInfo).build();
    }

}